package com.example.smartcampus;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    private static MyService instance1;

    public static MyService getInstance() {
        return instance1;
    }

    public MyService() {
    }

    private MediaPlayer mediaPlayer;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    @Override
    public void onCreate() {
        super.onCreate();
        if (instance1 == null) {
            MyService.instance1 = this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (mediaPlayer != null) mediaPlayer.stop();
        String songFileName = intent.getStringExtra("songFileName");
        @SuppressLint("DiscouragedApi") int SongId = getResources().getIdentifier(songFileName, "raw", getPackageName());
        mediaPlayer = MediaPlayer.create(getApplicationContext(), SongId);
        mediaPlayer.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        mediaPlayer.stop();
        super.onDestroy();

    }
}